<style>
</style>
<template>
  <list height="100%" :data="list_data" :cols="list_cols" row-key="id" :load="get_list" lazy :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
    <template #layer_info="{row}">
      <span>{{row.layer_info}}</span>
    </template>
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
      <el-button @click="to_team(row)" size="small">团队结构</el-button>
    </template>
  </list>
</template>
<script>
import {iframe, in_right_admin} from "@/js/tool.js";
import {_config, decode_config} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    let sex_config = _config('status_alias_map.user.sex');
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type: 'hidden', order:true},
      layer_info: {name:'layer_info', title:'层级', type: 'layer_info',align:'left', width:'200px'},
      head_img:{name:'head_img', label:'头像', width:'45px', type:'file_img'},
      phone:{name:'phone', label:'电话号码', width:'120px'},
      nickname:{name:'nickname', label:'昵称', width:'120px'},
      level:{name:'level', label:'等级', width:'120px',type:'select_one',config:[]},
      sex:{name:'sex', label:'性别', width:'40px',type:'select_one',config:sex_config},
      money:{name:'money', label:'余额', width:'75px'},
      last_time:{name:'last_time', label:'最后登录时间', width:'165px'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'注册时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'140px', type:'action', fixed:'right'};

    return {
      list_data: [],
      list_cols: list_cols,
    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('User/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('user/edit?'+s94Mjs.param_encode(param))
    },
    to_team(row){
      iframe(`user/team?id=${row.id}&nickname=${row.nickname}`);
    },
    get_list(row, treeNode, resolve){
      row = row || {id:this.$get('id'), layer:0}
      ajax({url:'/admin/user/list?where[parent_id][eq]='+row.id, loading:true},function(res){
        res.data.list.forEach(function (v){
          v.layer = row.layer+1;
          v.layer_info = '第'+v.layer+'层';
          v.hasChildren = true;
        })
        resolve(res.data.list);
        return false;
      })
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    this.get_list(false, {}, function (list){
      app.list_data = list;
    });
    ajax({url:'/admin/user_level/list?order[id]=asc',cache:true},(res)=>{
      let level_config = decode_config(res.data.list, {content_key:'name', value_key:'level'});
      this.list_cols.level.config = level_config;
      return false;
    })
  }
}
</script>
